import 'package:fluttertoast/fluttertoast.dart';
import 'package:jpush_flutter/jpush_flutter.dart';
import 'package:sui_shou_pai/Utils/storage_util.dart';
import 'package:sui_shou_pai/chat/entity/chat_record.dart';
import 'package:sui_shou_pai/chat/entity/employee_model.dart';
import 'package:sui_shou_pai/chat/entity/global.dart';
import 'package:sui_shou_pai/chat/entity/message_model.dart';
import 'package:sui_shou_pai/network/http_config.dart';
import 'package:sui_shou_pai/network/http_request.dart';
import 'package:sui_shou_pai/utils/aes_util.dart';

class InitMap {
  static Map<String, List<ChatRecord>> recordsMap = {};

  static Map<String, MessageModel> messageMap = {};

  static Map<String, dynamic> employeeMap = {};

  static JPush jPush = new JPush();

  static initGlobalMap() {
    initData();
    initJPush();
  }

  /**
   * 极光推送
   */
  static Future<void> initJPush() async {
    jPush.setup(
        appKey: "2955f1df53ba83f2e437f41d",
        channel: "theChannel",
        production: false,
        debug: true);

    jPush.applyPushAuthority(
        new NotificationSettingsIOS(sound: true, alert: true, badge: true));

    jPush.getRegistrationID().then((rid) {
      print("获得的注册id为：" + rid);
    });

    jPush.setTags([Global.me]).then((map) {
      var tags = map['tags'];
      print("设置标签成功: $map $tags");
    }).catchError((error) {
      print("设置标签错误: $error");
    });
  }

  static Future<void> initData() async {
    HttpRequest.request("$BASE_URL/employee/selectAll",
            method: "get", token: await StorageUtil.getStringItem("token"))
        .then((value) {
      if (value.data["code"] == 200) {
        List<dynamic> result = value.data["result"];
        for (var i = 0; i < result.length; i++) {
          MessageModel messageModel = MessageModel.formMap(result[i]);
          getUnreadNum(messageModel.id);
          List list = List<MapEntry<String, MessageModel>>();
          list.add(MapEntry(messageModel.id, messageModel));
          Iterable iterable = list.reversed;
          InitMap.messageMap.addEntries(iterable);
          getRecords(messageModel.id);
        }
      } else {
        Fluttertoast.showToast(msg: "加载失败");
      }
    });
  }

  static Future<void> getUnreadNum(String otherId) async {
    HttpRequest.request("$BASE_URL/chat-record/getUnreadNum",
            method: "get",
            params: {
              "me": AesUtil.aesEncrypt(Global.me),
              "other": AesUtil.aesEncrypt(otherId)
            },
            token: await StorageUtil.getStringItem("token"))
        .then((value) {
      if (value.data["code"] == 200) {
        String unreadNum = value.data["result"].toString();
        MessageModel messageModel = InitMap.messageMap[otherId];
        messageModel.setNum(unreadNum);
        InitMap.messageMap.update(otherId, (value) => messageModel);
      } else {
        Fluttertoast.showToast(msg: "数量加载失败");
      }
    });
  }

  static Future<void> getRecords(String otherId) async {
    HttpRequest.request("$BASE_URL/chat-record/queryRecords",
            method: "get",
            params: {
              "me": AesUtil.aesEncrypt(Global.me),
              "other": AesUtil.aesEncrypt(otherId)
            },
            token: await StorageUtil.getStringItem("token"))
        .then((value) {
      if (value.data["code"] == 200) {
        List<dynamic> result = value.data["result"];
        List<ChatRecord> records = [];
        for (var i = 0; i < result.length; i++) {
          ChatRecord article = ChatRecord.formMap(result[i]);
          records.add(article);
        }
        List list = List<MapEntry<String, List<ChatRecord>>>();
        list.add(MapEntry(otherId, records));
        Iterable iterable = list.reversed;
        InitMap.recordsMap.addEntries(iterable);
      } else {
        Fluttertoast.showToast(msg: "数量加载失败");
      }
    });
  }

  static Future<void> getEmployeeList() async {
    HttpRequest.request("$BASE_URL/employee/selectDE",
            method: "get", token: await StorageUtil.getStringItem("token"))
        .then((value) {
      if (value.data["code"] == 200) {
        employeeMap = value.data["result"];
        print(employeeMap);
        List<EmployeeModel> employee = [];
        // for(int i=0;i<result.length;i++){
        //     EmployeeModel employeeModel = EmployeeModel.formMap(result[i]);
        //     employee.add(employeeModel);
        //  }
      } else {
        Fluttertoast.showToast(msg: "数量加载失败");
      }
    });
  }
}
